﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Top_Manager_Invoice_Cancel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ASPxLabel2.Visible = false;
        ASPxLabel3.Visible = false;
        Session["current"] = "Cancle Invoice";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }
    }

    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        Boolean result2 = false;
        Boolean result3 = false;
        List<Bestlife.ERP.Entities.E_Sales> listinvoice = new List<Bestlife.ERP.Entities.E_Sales>();
        string code;
        string proid;
        string Qty;
        int i = ASPxGridView1.VisibleRowCount;

        List<Object> itemid1 = ASPxGridView1.GetSelectedFieldValues("Pro_ID");
        List<Object> itemid = ASPxGridView1.GetSelectedFieldValues("Code");
        List<Object> itemid2 = ASPxGridView1.GetSelectedFieldValues("Qty");

        //List<Object> itemid4 = ASPxGridView1.GetSelectedFieldValues("Sto_ID");
        int j = itemid.Count;

        string user = Session["username"].ToString();
        if (ASPxComboBox1.SelectedItem != null)
        {

            string rid = ASPxComboBox1.SelectedItem.ToString();
            if (itemid.Count == 0 || itemid1.Count == 0 || itemid2.Count == 0)
            {
                ASPxLabel2.Visible = true;
                ASPxLabel2.Text = "Please select order items ";
            }
            else
            {
                foreach (object selectitemid in itemid)
                {
                    code = selectitemid.ToString();

                }

                foreach (object selectitemid in itemid1)
                {
                    proid = selectitemid.ToString();

                }
                foreach (object selectitemid in itemid2)
                {
                    Qty = selectitemid.ToString();

                }
                for (int k = 0; k < itemid.Count; k++)
                {
                    Bestlife.ERP.Entities.E_Sales invoice = new Bestlife.ERP.Entities.E_Sales();
                    invoice.Proid = itemid1[k].ToString();
                    invoice.Qty = itemid2[k].ToString();
                    invoice.Code = itemid[k].ToString();
                    listinvoice.Add(invoice);



                }

                Boolean result1 = Bestlife.ERP.BusinessRules.B_Sales.upd_recipt(rid,user);
                if (result1)
                {
                    //foreach (Bestlife.ERP.Entities.E_Sales sales in listinvoice)
                    //{
                    if (ASPxRadioButton1.Checked)
                    {
                        result2 = Bestlife.ERP.BusinessRules.B_Sales.upd_stock(listinvoice, user, itemid.Count);
                    }
                    if (ASPxRadioButton2.Checked)
                    {
                        result3 = Bestlife.ERP.BusinessRules.B_Sales.upd_stockkandy(listinvoice, user, itemid.Count);
                    }
                    //}
                }
                if (result1 == true && result3 == true)
                {
                    Bestlife.ERP.DataAccess.D_Sales.del_receipt(rid);
                    ASPxLabel3.Visible = true;
                    ASPxLabel3.Text = "Recipt Cancelled Sucessfully!!";
                }
                if (result1 == true && result2 == true)
                {
                    Bestlife.ERP.DataAccess.D_Sales.del_receipt(rid);
                    ASPxLabel3.Visible = true;
                    ASPxLabel3.Text = "Recipt Cancelled Sucessfully!!";
                }
            }
        }
    }

    protected void ASPxComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string rid = ASPxComboBox1.Text;
        Bestlife.ERP.Entities.E_Sales sales = new Bestlife.ERP.Entities.E_Sales();
        sales = Bestlife.ERP.BusinessRules.B_Sales.sales(rid);
        ASPxTextBox1.Text = sales.Totprice;
        ASPxTextBox2.Text = sales.Totpv;
        ASPxTextBox3.Text = sales.Totbv;
        ASPxTextBox4.Text = sales.Cashiername;
        ASPxTextBox5.Text = sales.Date;
        ASPxTextBox6.Text = sales.D_name1;

    }
}